/*
	Programação Concorrente
	Trabalho 2
	Turma A

	Adilson Abreu Couto Rapozo Junior ( n. USP: 6402336 )
	Luiz Alfredo Bertoni ( n. USP: 6418834 )
	Rodrigo Fernandes da Costa ( n. USP: 6793073 )
*/

#include "main.h"

int readArguments ( int argc , char ** argv , int * pWord , int * pPhrase , int * pPrime , char * pFileName ) {

	int i ;
	FILE * pFile ;

	* pWord = 0 ;
	* pPhrase = 0 ;
	* pPrime = 0 ;

	/* Min number of arguments check. */

	if ( argc < 3 ) {

		showHelp ( ) ;
		return 0 ;
	}

	/* Check if the file can be read. */

	pFile = fopen ( argv [ argc - 1 ] , "r" ) ;

	if ( pFile == NULL ) {

		printf ( "Could not open %s.\n" , argv [ argc - 1 ] ) ;
		return 0 ;
	}

	fclose ( pFile ) ;

	strcpy ( pFileName , argv [ argc - 1 ] ) ;

	for ( i = 1 ; i < argc - 1 ; ++ i ) {

		if ( strcmp ( "-pw" , argv [ i ] ) == 0 ) {

			* pWord = 1 ;

		} else if ( strcmp ( "-pp" , argv [ i ] ) == 0 ) {

			* pPhrase = 1 ;

		} else if ( strcmp ( "-p" , argv [ i ] ) == 0 ) {

			* pPrime = 1 ;

		} else {

			showHelp ( ) ;
			return 0 ;
		}
	}

	if ( ! * pWord && ! * pPhrase ) {

		showHelp ( ) ;
		return 0 ;
	}

	if ( * pPrime && ! * pWord  ) {

		showHelp ( ) ;
		return 0 ;
	}

	return 1 ;
}

void showHelp ( void ) {

	printf ( "Usage: a.out [-pw [-pp] [-p] | -pp] FILE\n" ) ;
	printf ( "-pw\tCheck if words are palindromes.\n" ) ;
	printf ( "-pp\tCheck if phrases are palindromes.\n" ) ;
	printf ( "-p\tIf the word is a palindrome, check if the sum of the characters values is a prime number.\n" ) ; 
}

